/**
 * @Title: BlogServiceImpl.java
 * @Package com.zll.ccp.service.blog
 * @author zhanglinlu
 * @date 2019年5月4日 下午3:33:50
 */
package com.zll.ccp.service.blog.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zll.ccp.mapper.IBlogCommentMapper;
import com.zll.ccp.mapper.IBlogMapper;
import com.zll.ccp.mapper.IBlogReplyMapper;
import com.zll.ccp.mapper.IBlogTypeMapper;
import com.zll.ccp.mapper.IUserMapper;
import com.zll.ccp.model.blog.Blog;
import com.zll.ccp.model.blog.BlogComment;
import com.zll.ccp.model.blog.BlogReply;
import com.zll.ccp.model.blog.BlogType;
import com.zll.ccp.service.blog.IBlogService;

/**
 * @ClassName: BlogServiceImpl
 * @Description: 博客service实现类
 * @author zhanglinlu
 * @date 2019年5月4日 下午3:33:50
 * 
 */
@Service
@Transactional
public class BlogServiceImpl implements IBlogService {
    @Autowired
    private IBlogMapper blogMapper;
    @Autowired
    private IBlogCommentMapper blogCommentMapper;
    @Autowired
    private IBlogReplyMapper blogReplyMapper;
    @Autowired
    private IBlogTypeMapper blogTypeMapper;
    @Autowired
    private IUserMapper IUserMapper;

    /* (non-Javadoc)
     * @param id
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getInitBlogData(java.lang.String)
     */
    @Override
    public Blog getBlogById(String id) {
        if (StringUtils.isBlank(id)) {
            return new Blog();
        }
        return blogMapper.getBlogById(id);
    }

    /* (non-Javadoc)
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getBlogTypeList()
     */
    @Override
    public List<BlogType> getBlogTypeList() {
        return blogTypeMapper.getBlogTypeList();
    }

    /* (non-Javadoc)
     * @param blog
     * @see com.zll.ccp.service.blog.IBlogService#addBlog(com.zll.ccp.model.blog.Blog)
     */
    @Override
    public void addBlog(Blog blog) {
        blogMapper.addBlog(blog);
    }

    /* (non-Javadoc)
     * @param blog
     * @see com.zll.ccp.service.blog.IBlogService#updateBlog(com.zll.ccp.model.blog.Blog)
     */
    @Override
    public void updateBlog(Blog blog) {
        blogMapper.updateBlog(blog);
    }

    /* (non-Javadoc)
     * @param param
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getBlogListByFilter(java.util.Map)
     */
    @Override
    public PageInfo<Map<String, Object>> getBlogListByFilter(Map<String, Object> param, int nowPage, int pagesize) {
        PageHelper.startPage(nowPage, pagesize);
        List<Map<String, Object>> blogList = blogMapper.getBlogListByFilter(param);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(blogList);
        return pageInfo;
    }

    /* (non-Javadoc)
     * @param param
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getBlogCountByFilter(java.util.Map)
     */
    @Override
    public long getBlogCountByFilter(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return blogMapper.getBlogCountByFilter(param);
    }

    /* (non-Javadoc)
     * @param name
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getUserIdsByName(java.lang.String)
     */
    @Override
    public List<String> getUserIdsByName(String name) {
        return IUserMapper.getUserIdByNameList(name);
    }

    /* (non-Javadoc)
     * @param id
     * @see com.zll.ccp.service.blog.IBlogService#deleteBlogByid(java.lang.String)
     */
    @Override
    public void deleteBlogByid(String id) {
        blogMapper.deleteBlogByid(id);
    }

    /* (non-Javadoc)
     * @param id
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getBlogDTOById(java.lang.String)
     */
    @Override
    public Map<String, Object> getBlogDTOById(String id) {
        Map<String, Object> blogDTOs = blogMapper.getBlogDTOById(id);
        return blogDTOs;
    }

    /* (non-Javadoc)
     * @param blogComment
     * @see com.zll.ccp.service.blog.IBlogService#addBlogComment(com.zll.ccp.model.blog.BlogComment)
     */
    @Override
    public void addBlogComment(BlogComment blogComment) {
        blogCommentMapper.addBlogComment(blogComment);
    }

    /* (non-Javadoc)
     * @param blogId
     * @return
     * @see com.zll.ccp.service.blog.IBlogService#getBlogCommentsById(java.lang.String)
     */
    @Override
    public List<Map<String, Object>> getBlogCommentsById(String blogId) {
        List<Map<String, Object>> blogComments = blogCommentMapper.getBlogCommentsById(blogId);
        List<String> commentIds = new ArrayList<>();
        if (blogComments != null && blogComments.size() != 0) {
            for (Map<String, Object> comment : blogComments) {
                commentIds.add(comment.get("id").toString());
            }
            // 获取回复
            List<Map<String, Object>> replyDtos = blogReplyMapper.getReplyDtoByCommentId(commentIds);
            // 匹配
            for (int index = 0; index < commentIds.size(); index++) {
                String id = commentIds.get(index);
                List<Map<String, Object>> replys = new ArrayList<>();
                for (Map<String, Object> reply : replyDtos) {
                    if (StringUtils.equals(id, reply.get("commentId").toString())) {
                        replys.add(reply);
                    }
                }
                blogComments.get(index).put("replys", replys);
            }
        }
        return blogComments;
    }

    /* (non-Javadoc)
     * @param blogReply
     * @see com.zll.ccp.service.blog.IBlogService#addBlogReply(com.zll.ccp.model.blog.BlogReply)
     */
    @Override
    public void addBlogReply(BlogReply blogReply) {
        blogReplyMapper.addBlogReply(blogReply);
    }

    /* (non-Javadoc)
     * @param id
     * @see com.zll.ccp.service.blog.IBlogService#deleteCommentById(java.lang.String)
     */
    @Override
    public void deleteCommentById(String id) {
        blogCommentMapper.deleteCommentById(id);
    }

    /* (non-Javadoc)
     * @param id
     * @see com.zll.ccp.service.blog.IBlogService#deleteReplyById(java.lang.String)
     */
    @Override
    public void deleteReplyById(String id) {
        blogReplyMapper.deleteReplyById(id);
    }

}
